using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class AchievementsManager : MonoBehaviour
{
	public static AchievementsManager instanse;

	public List<Achievement> allAchievements = new List<Achievement>();

	public List<KillAchievement> allKillAchievements = new List<KillAchievement>();

	public List<DestroyAchievement> allDestroyAchievements = new List<DestroyAchievement>();

	public List<Achievement> achievementUnlokedMessagesList = new List<Achievement>();

	public bool achievementUnlokedMessage;

	public GameObject achivmentUnlokedPanelGO;

	private void Awake()
	{
		instanse = this;
	}

	private void Start()
	{
		if (!achivmentUnlokedPanelGO)
		{
			if (SceneManager.GetActiveScene().name == "Lobby")
			{
				achivmentUnlokedPanelGO = controllerMenu.thisScript.panelAchievement;
			}
			else
			{
				achivmentUnlokedPanelGO = GameTopWindow.instance.panelAchievement;
			}
		}
	}

	public void ShowAchievementUnlokedPanel(Achievement achievement)
	{
		achievementUnlokedMessagesList.Add(achievement);
		TryShowAchievementUnlokedPanel();
	}

	public void TryShowAchievementUnlokedPanel()
	{
		if (achievementUnlokedMessage || achievementUnlokedMessagesList.Count <= 0)
		{
			return;
		}
		Achievement achievement = achievementUnlokedMessagesList[0];
		achievementUnlokedMessagesList.Remove(achievement);
		if (!achivmentUnlokedPanelGO)
		{
			if (SceneManager.GetActiveScene().name == "Lobby")
			{
				achivmentUnlokedPanelGO = controllerMenu.thisScript.panelAchievement;
			}
			else
			{
				achivmentUnlokedPanelGO = GameTopWindow.instance.panelAchievement;
			}
		}
		achivmentUnlokedPanelGO.transform.localPosition = new Vector3(-7f, -450f, 0f);
		StartCoroutine(achivmentUnlokedPanelGO.GetComponent<AchivmentUnlokedPanel>().Init(achievement, 6.75f));
	}

	public void GiveAchievementReward(Reward achievementReward, Vector3 position)
	{
		BCWDebug.Log(string.Concat("Reward group: ", achievementReward.rewardGroup, " Reward amount: ", achievementReward.rewardAmount, " Reward ID: ", achievementReward.rewerdID));
		switch (achievementReward.rewardGroup)
		{
		case RewardGroups.Non:
			break;
		case RewardGroups.Cash:
			settings.ShowSpecialLabelWithAnimation(achievementReward.rewardAmount, position, false);
			break;
		case RewardGroups.Coins:
			settings.ShowLabelWithAnimation(achievementReward.rewardAmount, position, false);
			break;
		case RewardGroups.Product:
			MarketController.Instance.BuyProduct(achievementReward.rewerdID, true);
			break;
		case RewardGroups.ProductConsumable:
			MarketController.Instance.BuyProduct(achievementReward.rewerdID, true, achievementReward.rewardAmount);
			break;
		}
	}

	public void SaveAllAchievementsStates()
	{
		foreach (Achievement allAchievement in allAchievements)
		{
			allAchievement.SaveAchievementState();
		}
	}

	private void OnDestroy()
	{
		if (SceneManager.GetActiveScene().name == "Lobby")
		{
			SaveAllAchievementsStates();
		}
	}

	public void UnsetAllAchievements()
	{
		foreach (Achievement allAchievement in allAchievements)
		{
			allAchievement.Unset();
			allAchievement.Init();
		}
		BCWStorage.Set(AllUnitaryAchievements.instance.normalQuestAchievementsInitKey, false, true);
	}
}
